package com.aiyige.page.my.wallet;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.eventbus.EventSetWalletPwdSuccess;
import com.aiyige.utils.ToastX;
import com.aiyige.utils.widget.PayPwdEditText;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.vondear.rxtools.RxKeyboardTool;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 设置钱包-设置密码
 */
@Route(path = ARouterConfig.WalletSetPwdPage1, extras = ARouterConfig.EXTRA_NEED_LOGIN)
public class WalletSetPwdPage1 extends BaseActivity {


    @BindView(R.id.tv_next)
    TextView tvFinish;
    @BindView(R.id.tv_setpwd_hint)
    TextView tvSetpwdHint;
    @BindView(R.id.pay_pwd_edittext)
    PayPwdEditText payPwdEdittext;

    String password1;
    String password2;
    @Autowired
    public String code;
    @Autowired
    public boolean isChangePassword = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_wallet_set_password1);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        initTitleLayout(getString(isChangePassword?R.string.change_wallet_password:R.string.set_wallet_password));
        setAutoRegisterEventBus(true);
        /**
         * @param bgdrawable 背景drawable
         * @param pwdlength 密码长度
         * @param splilinewidth 分割线宽度
         * @param splilinecolor 分割线颜色
         * @param pwdcolor 密码字体颜色
         * @param pwdsize 密码字体大小
         */
        payPwdEdittext.initStyle(R.drawable.edit_num_bg, 6, 0.5f, R.color.zwcolor_gray_bfbfbf, R.color.zwcolor_black_111111, 32);
        payPwdEdittext.setOnTextFinishListener(new PayPwdEditText.OnTextFinishListener() {
            @Override
            public void onFinish(String str) {//密码输入完后的回调
                password1 = str;
                tvFinish.setVisibility(View.VISIBLE);
                ARouter.getInstance()
                        .build(ARouterConfig.WalletSetPwdPage2)
                        .withString("code",code)
                        .withString("password1",password1)
                        .withBoolean("isChangePassword",isChangePassword)
                        .navigation();
            }
        });
        payPwdEdittext.setFocus();
        RxKeyboardTool.showSoftInput(this,payPwdEdittext.getEditText());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSetPwdSuccess(EventSetWalletPwdSuccess eventSetWalletPwdSuccess) {
        if (eventSetWalletPwdSuccess != null) {
            finish();
        }
    }

    private void checkNext() {
        password1 = payPwdEdittext.getPwdText();
        if (!TextUtils.isEmpty(password1) && password1.length() == 6) {
            tvFinish.setVisibility(View.VISIBLE);
            ARouter.getInstance()
                    .build(ARouterConfig.WalletSetPwdPage2)
                    .withString("code", code)
                    .withString("password1", password1)
                    .withBoolean("isChangePassword",isChangePassword)
                    .navigation();
        } else {
            ToastX.show("请输入6位数密码");
        }


    }

    @OnClick({R.id.tv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_next:
                checkNext();
                break;
        }
    }

    @OnClick(R.id.tv_next)
    public void onViewClicked() {
    }
}
